package engine.systems.ui.book
{
	import ash.core.Component;
	
	public class Book extends Component
	{
		internal var invalidate:Boolean = false;
		
		/**
		 * If true, the Book will ease to the target page based on its distance.
		 * If false, the Book will immediately flip to the target page.
		 */
		public var ease:Boolean = true;
		public var handler:Function;
		
		/**
		 * If ease is true, the Book will ease to the target page based on its distance times this rate.
		 */
		public var rate:Number = 4;
		
		private var _offsetX:Number;
		private var _offsetY:Number;
		
		private var _pageWidth:Number;
		private var _pageHeight:Number;
		
		private var _page:int;
		private var _numPages:int;
		private var _numColumns:int;
		
		public function Book(page:int = 1, numPages:int = 1, numColumns:int = 5, pageWidth:int = 100, pageHeight:Number = 100)
		{
			_page 		= page;
			_numPages 	= numPages;
			_numColumns	= numColumns;
			_pageWidth	= pageWidth;
			_pageHeight = pageHeight;
			
			this.recalculate();
		}
		
		public function get page():int { return _page; }
		public function set page(page:int):void
		{
			if(page < 1) page = 1;
			else if(page > _numPages) page = _numPages;
			
			if(this._page == page) return;
			this._page = page;
			
			this.recalculate();
		}
		
		public function get pageWidth():Number { return _pageWidth; }
		public function set pageWidth(pageWidth:Number):void
		{
			if(_pageWidth == pageWidth) return;
			
			_pageWidth = pageWidth;
			
			this.recalculate();
		}
		
		public function get pageHeight():Number { return _pageHeight; }
		public function set pageHeight(pageHeight:Number):void
		{
			if(_pageHeight == pageHeight) return;
			
			_pageHeight = pageHeight;
			
			this.recalculate();
		}
		
		public function get numPages():int { return _numPages; }
		public function set numPages(numPages:int):void
		{
			if(numPages < 1) numPages = 1;
			
			if(_numPages == numPages) return;
			
			_numPages = numPages;
			
			if(_page > _numPages) _page = _numPages;
			
			this.recalculate();
		}
		
		public function get numColumns():int { return _numColumns; }
		public function set numColumns(numColumns:int):void
		{
			if(numColumns < 1) 				numColumns = 1;
			else if(numColumns > _numPages) numColumns = _numPages;
			
			if(_numColumns == numColumns) return;
			
			_numColumns = numColumns;
			
			this.recalculate();
		}
		
		private function recalculate():void
		{
			var column:int = _page - 1;
			var row:int = 0;
			
			while(column > _numColumns - 1)
			{
				column -= _numColumns;
				row++;
			}
			
			_offsetX = -column * _pageWidth;
			_offsetY = -row * _pageHeight;
			
			this.invalidate = true;
		}
		
		/**
		 * OffsetX is only altered internally. It's used to specify the target x offset to reach a new page.
		 */
		public function get offsetX():int { return _offsetX; }
		
		/**
		 * OffsetY is only altered internally. It's used to specify the target y offset to reach a new page.
		 */
		public function get offsetY():int { return _offsetY; }
	}
}